#include "nuc.h"
#include "crc16.h"
#include <string.h>
static volatile uint32_t drop_message = 0;

static osThreadId_t thread_alert;

uint8_t nucbuf[20];


static void NUC_IdleCallback(void) {	
	osThreadFlagsSet(thread_alert,SIGNAL_NUC_RAW_REDY);
}

int8_t NUC_Init(NUC_t *nuc){
    if(nuc == NULL) return DEVICE_ERR_NULL;
    if((thread_alert = osThreadGetId()) == NULL ) return DEVICE_ERR_NULL;
   BSP_UART_RegisterCallback(BSP_UART_NUC,BSP_UART_IDLE_LINE_CB,
                            NUC_IdleCallback);
	
   return DEVICE_OK;
}

int8_t NUC_StartReceiving(NUC_t *nuc) {
  if (HAL_UARTEx_ReceiveToIdle_DMA(BSP_UART_GetHandle(BSP_UART_NUC),
                           (uint8_t *)nucbuf,
                           sizeof(nucbuf)) == HAL_OK)
    return DEVICE_OK;
  return DEVICE_ERR;
}

int8_t NUC_Restart(void) {
  __HAL_UART_DISABLE(BSP_UART_GetHandle(BSP_UART_NUC));
  __HAL_UART_ENABLE(BSP_UART_GetHandle(BSP_UART_NUC));
  return DEVICE_OK;
}

bool_t NUC_WaitDmaCplt(void) {
  return (osThreadFlagsWait(SIGNAL_NUC_RAW_REDY, osFlagsWaitAll, 0) ==
          SIGNAL_NUC_RAW_REDY);
}
int8_t NUC_ParserawData(NUC_t *n){
 	static union 
	{
		float x[3];
		char data[12];
	}instance;
  if(nucbuf[0]!=ID_CHASSIS_C) goto error;  //发送ID不是底盘
  else{
    n->Nucfor_chassis.data.recv_id = ID_CHASSIS_C;

    switch (nucbuf[1])
    {
    case NAVI:
       instance.data[3] = nucbuf[0];
       instance.data[2] = nucbuf[1];
       instance.data[1] = nucbuf[2];
       instance.data[0] = nucbuf[3];
      n->Nucfor_chassis.data.navi.vx = instance.x[0]; //
       instance.data[7] = nucbuf[4];
       instance.data[6] = nucbuf[5];
       instance.data[5] = nucbuf[6];
       instance.data[4] = nucbuf[7];
      n->Nucfor_chassis.data.navi.vy = instance.x[1];//
       instance.data[11] = nucbuf[8];
       instance.data[10] = nucbuf[9];
       instance.data[9] = nucbuf[10];
       instance.data[8] = nucbuf[11];
      n->Nucfor_chassis.data.navi.wz = instance.x[1];//
      break;
    case PICK:
       n->Nucfor_chassis.data.pick.cmd = nucbuf[0];
       instance.data[3] = nucbuf[1];
       instance.data[2] = nucbuf[2];
       instance.data[1] = nucbuf[3];
       instance.data[0] = nucbuf[4];
      n->Nucfor_chassis.data.pick.posy = instance.x[0]; //距离球的深度位置
       instance.data[7] = nucbuf[5];
       instance.data[6] = nucbuf[6];
       instance.data[5] = nucbuf[7];
       instance.data[4] = nucbuf[8];
      n->Nucfor_chassis.data.pick.angle = instance.x[1];//距离球中心角度值
       break;
    case PUT:

       break;
      

    }

     return DEVICE_OK;
  }
    error:
    drop_message++;
    return DEVICE_ERR;

}

int8_t NUC_ParseforChassis(CMD_NUC_t *cmd,NUC_t *from_nuc){
     (void)cmd;
     if(cmd ==NULL) return DEVICE_ERR_NULL;
     if(from_nuc ==NULL) return DEVICE_ERR_NULL;
     cmd->status_fromnuc = from_nuc->status;
      switch (from_nuc->status)
      {
        case NAVI:
        cmd->navi.vx =from_nuc->Nucfor_chassis.data.navi.vx;
        cmd->navi.vx =from_nuc->Nucfor_chassis.data.navi.vx;
        cmd->navi.wz =from_nuc->Nucfor_chassis.data.navi.vy;
        break;
        case PUT:
        cmd->put.angle =from_nuc->Nucfor_chassis.data.put.angle;
        cmd->put.pos =from_nuc->Nucfor_chassis.data.put.pos;
        break;
        case PICK:
        cmd->pick.angle =from_nuc->Nucfor_chassis.data.pick.angle;
        cmd->pick.cmd =from_nuc->Nucfor_chassis.data.pick.cmd;
        cmd->pick.posy =from_nuc->Nucfor_chassis.data.pick.posy;
        break;
      }
      return DEVICE_OK;

}



int8_t NUC_HandleOffline(NUC_t *nuc, CMD_NUC_t *cmd_nuc) {
  if (nuc == NULL) return DEVICE_ERR_NULL;
  if (cmd_nuc == NULL) return DEVICE_ERR_NULL;

  memset(&(nuc->Nucfor_chassis), 0, sizeof(nuc->Nucfor_chassis));
  memset(cmd_nuc, 0, sizeof(*cmd_nuc));
  return 0;
}



